﻿using Microsoft.Practices.Prism.ViewModel;
using Neudesic.Pulse.Entities;
using Neudesic.Pulse.Services;
using System;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text.RegularExpressions;

namespace Neudesic.Pulse.ViewModels
{
    /// <summary>
    /// Viewmodel that wraps an activity instance.
    /// </summary>
    /// <remarks>
    /// Because this viewmodel is largely read-only and we are only using it to extend the existing domain model, we don't replicate properties here and pass them to the model,
    /// we expose the model directly as a property, and add our own supplemental properties where needed.  Properties specific to the view are located here, whereas functionality
    /// that extends the model in general will be found in extensions methods.
    /// </remarks>
    public class ActivityViewModel : NotificationObject
    {

        private IPulseGateway pulse;
        private Entity me;

        /// <summary>
        /// Gets or sets the activity.
        /// </summary>
        /// <value>The activity.</value>
        public Activity Activity { get; private set; }

        /// <summary>
        /// Gets or sets the pulse web address.
        /// </summary>
        /// <remarks>
        /// This is only used for demo purposes and should not exist in a real application.  This demo application links to the Pulse website instead of providing a complete interaction with the view data.
        /// </remarks>
        /// <value>The pulse web address.</value>
        public string PulseWebAddress { get; private set; }

        /// <summary>
        /// Gets the avatar.
        /// </summary>
        /// <value>The avatar.</value>
        public string AvatarUri
        {
            get
            {
                return UriUtility.GetSizedAvatarUri(Activity.Author.Image, 20);
            }
        }

        public string ActivityLabel
        {
            get
            {
                return Activity.IsComment ? " commented on " : " to ";
            }
        }

        public bool NeedsActivityLabelDisplayed
        {
            get
            {
                if (Activity.To != null && Activity.Author.IsUser)
                {
                    return !Activity.Author.IsMe(Activity.To);
                }
                return Activity.To != null;
            }
        }

        public string ToLabel
        {
            get
            {
                return Activity.IsComment ? string.Format("{0}'s post", Activity.To.Feed.Name.Trim()) : Activity.To.Name;
            }
        }

        public bool IsLikedByMe
        {
            get { return Activity.Likes.Any(p => p.IsMe(me)); }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ActivityViewModel" /> class.
        /// </summary>
        /// <param name="activity">The activity.</param>
        public ActivityViewModel(Activity activity, IPulseGateway pulse)
        {            
            this.Activity = activity;
            this.pulse = pulse;
            this.me = pulse.GetMe();

            this.PulseWebAddress = ConfigurationService.PulseWebAddress;
        }
    }
}
